<?php
namespace api\tests;

use Yii;
use api\tests\ApiTester;

/**
 * 1. 尝试没有access_token访问.
 * 2. 尝试访问全部商品.
 * 3. 尝试访问单个商品.
 * 4. 尝试更新商品.
 * 5. 尝试创建商品.
 * 6. 尝试删除商品.
 */
class ShopsCest
{
    protected $token;

    /**
     * 验证授权Authorization
     */
    public function _before(ApiTester $I)
    {
        $this->token = Yii::$app->cache->getOrSet('access_token', function () use ($I) {
            $I->sendGET('/v1/auth', [ 'uid' => '770396' ]);
            return json_decode($I->grabResponse());
        }, 7200);
        $I->amBearerAuthenticated($this->token->access_token);
    }

    /**
     * 尝试没有access_token访问
     */
    public function tryNoTokenToVisit(ApiTester $I)
    {
        $I->haveHttpHeader('Authorization', 'Bearer ' . $this->token->access_token);
        $I->deleteHeader('Authorization');
        $I->sendGET('/v1/shops/3358');
        $I->seeResponseContainsJson(['status' => 401]);
    }

    /**
     * 尝试访问全部商品
     */
    public function tryToGetAllTheShops(ApiTester $I)
    {
        $I->sendGET('/v1/shops');
        $I->seeResponseContainsJson(['success' => true]);
        sleep(1);
    }
    /**
     * 尝试访问单个商品
     */
    public function tryToGetASignleShop(ApiTester $I)
    {
        $I->sendGET('/v1/shops/3358');
        $I->seeResponseContainsJson(['success' => true]);
        sleep(1);
    }

    /**
     * 更新商品
     * @throws cannot be update
     */
    public function tryToUpdateShop(ApiTester $I)
    {
        $I->sendPATCH('/v1/shops/3358', [ ]);
        $I->seeResponseContains('An Error occurred while handling another error');
        //$I->see('An internal server error occurred');
        sleep(1);
    }

    /**
     * 尝试创建商品
     * @throws cannot be create 
     */
    public function tryToCreateShop(ApiTester $I)
    {
        $I->sendPOST('/v1/shops', [ ]);
        $I->seeResponseContains('An Error occurred while handling another error');
        sleep(1);
    }

    /**
     * 尝试删除商品
     * @throws cannot be delete shop item
     */
    public function tryToDeleteShop(ApiTester $I)
    {
        $I->sendDELETE('/v1/shops/3358');
        $I->seeResponseContains('An Error occurred while handling another error');
        sleep(1);
    }
}
